

var Office = {
    id: "OfficeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

Office.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
       /* {title: 'id', field: 'id', align: 'center', valign: 'middle',sortable: true,visible: false},*/
        {title: '公司名称', field: 'name', align: 'left', valign: 'middle', sortable: true},
        {title: '公司代码', field: 'compCode', align: 'center', valign: 'middle', sortable: true},
        {title: '英文名称', field: 'enname', align: 'center', valign: 'middle', sortable: true},
        {title: '公司简称', field: 'simplename', align: 'center', valign: 'middle', sortable: true}, 
        {title: '联系地址', field: 'address', align: 'center', valign: 'middle', sortable: true},
        {title: '联系电话', field: 'phone', align: 'center', valign: 'middle', sortable: true}
        
        ];
};

/**
 * 检查是否选中
 */
Office.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Office.seItem = selected;
        return true;
    }
};


Office.singleCheck = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
    	if(selected.length > 1){
            Feng.info("请先选中表格中的一条记录进行操作！");
            return false;
        }else{
        	Office.seItem = selected[0];
        	console.log(Office.seItem);
            return true;
        }
        
    }
};

/**
 * 搜索重置
 */
Office.resetSearch = function () {
    $("#condition").val("");
    Office.search();
}

Office.openAddOffice = function () {
    var index = layer.open({
        type: 2,
        title: '添加公司',
        area: ['100%', '100%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/sys/sysOffice/add'
    });
    this.layerIndex = index;
};


Office.openOfficeDetail = function () {
    if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '修改公司',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/sys/sysOffice/update/' + Office.seItem.id
        });
        this.layerIndex = index;
    }
};

Office.openOfficeView = function(){
	if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '修改公司',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/sys/sysOffice/view/' + Office.seItem.id
        });
        this.layerIndex = index;
    }
}
Office.openRelationView = function(){
	if (this.singleCheck()) {
		
		var type = Office.seItem;
		console.log(type);
		if(type == "1"){
			Feng.err("外勤公司不允许关联");
			return;
		}
        var index = layer.open({
            type: 2,
            title: '关联外勤公司',
            area: ['400px', '500px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/sys/sysOffice/toRelation/' + Office.seItem.id
        });
        this.layerIndex = index;
    }
}

Office.delete = function () {
    if (this.singleCheck()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/sys/sysOffice/save/delete", function (data) {
            	if(data.resultCode==1){
            		Feng.success("删除成功!");
            		Office.table.refresh();
            	}else{
            		Feng.error(data.msg + "!");
            	}        
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("officeId",Office.seItem.id);
            ajax.start();
        };
        Feng.confirm("是否刪除选中的公司以及其下属公司?", operation);
    }
};

/**
 * 查看关联关系
 */
Office.showOffice = function () {
    if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '关联关系',
            area: ['400px', '300px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/sys/sysOffice/vehicle_office/' + Office.seItem.id
        });
        this.layerIndex = index;
    }
}

/**
 * 查询机构列表
 */
Office.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Office.table.refresh();    
};

$(function () {
    var defaultColunms = Office.initColumn();
    var paramsId =[
    	{
    		"key":"condition",
    		"type":"input",
    		"id":"condition"
    	}
    ];
    var table = new BSTreeTable(Office.id, "/sys/sysOffice/list", defaultColunms,paramsId);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(true);
    table.init();
    Office.table = table;
});


